<script lang="ts" setup>
import useStore from '@/store';
import { storeToRefs } from 'pinia';
import { ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'  //图片组件
import GoodsName from './components/goods-name.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message/index'
import { CartItem } from '@/types/cart'
const { goods , cart } = useStore()
const route = useRoute()
watchEffect(()=>{
  const id = route.params.id as string
  if( id ){
    goods.clearGoods()  //切其他页面，在回到当前页面的默认禁用不会丢失
    goods.getGoodsList(id)
  }
  // console.log(goods.info);
})
const { info } = storeToRefs(goods)
//sku组件----------------------------------------------------------
// 保存skuId
const currentSkuId = ref('')
const changeSku=(skuId:string)=>{
//  console.log(skuId);
 currentSkuId.value = skuId
 const sku = info.value.skus.find(sku=>sku.id===skuId)
 
 if(sku){
  info.value.inventory = sku.inventory  //更新库存
  info.value.price = sku.price  //更新价格
  info.value.oldPrice = sku.oldPrice //更新旧的价格
 }
}
//数量选择组件------------------------------------------------------
const count =ref(1)
// -------------------------------------------------------
const addCart = async () => {
  if (!currentSkuId.value) {
    return Message.warning('请选择完整的商品信息')
  }
   // 根据 currentSkuId.value 去 info.value.skus 找, 找对应的 sku
  const sku = info.value.skus.find((item) => item.id === currentSkuId.value)
  const arr = sku?.specs.map((item) => `${item.name}:${item.valueName}`)
  const attrsText = arr?.join(' ')
  await cart.addCart({
    // 本地添加
    id: info.value.id, // 商品id
    name: info.value.name, // 商品名称
    picture: info.value.mainPictures[0], // 商品的图片
    price: info.value.price, // 价格
    count: count.value, // 数量
    skuId: currentSkuId.value, // skuId
    attrsText: attrsText || '', // 规格描述 => 黑色 国产 41
    selected: true, // 是否选中
    nowPrice: info.value.price, // 现价
    stock: info.value.inventory, // 库存
    isEffective: true, // 是否有效
  } as CartItem)

  Message.success('添加成功')
}
</script>



<template>
  <div class="xtx-goods-page" v-if="info.categories">
    <div class="container">
      <!-- 面包屑 -->
      <Transition name="fade">
        <XtxBread v-if="info.categories">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${info.categories[1].id}`">
          {{ info.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">
          {{ info.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>{{ info.name }}</XtxBreadItem>
      </XtxBread>
      <XtxSkeleton 
      v-else 
      :width="1240" 
      :height="600"
      bg="pink"
      >
      </XtxSkeleton>
      </Transition>

       <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="info.mainPictures"></GoodsImage>
          <!-- 图片下面信息 -->
          <GoodsSales :goods="info"></GoodsSales>
        </div>
        <div class="spec">
          <!-- 右侧信息 -->
           <GoodsName :goods="info"/>
            <!-- 规格组件 -->
           <GoodsSku :goods="info" 
           skuId="1379052170887827457"
           @changeSku="changeSku"></GoodsSku>
           <!-- 数量选择组件  -->
           <XtxNumbox v-model:count="count"
           :min="2" :max="10"></XtxNumbox>
           <!-- 购物车按钮 -->
           <XtxButton 
           @click="addCart"
           style="margin-top: 20px" 
           type="primary">
           加入购物车
           </XtxButton>
        </div>
      </div>

        <div class="goods-footer">
        <div class="goods-article">
        <!-- 商品+评价 -->
        <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail :goods="info" />  
        </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <GoodsHot :type="1" />
            <GoodsHot :type="2" />
            <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>

</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>

